package com.softeem.xyblogmaster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.xyblogmaster.domain.Articles;
import com.softeem.xyblogmaster.domain.Categories;
import com.softeem.xyblogmaster.mapper.ArticlesMapper;
import com.softeem.xyblogmaster.service.ArticlesService;
import com.softeem.xyblogmaster.service.CategoriesService;
import com.softeem.xyblogmaster.mapper.CategoriesMapper;
import com.softeem.xyblogmaster.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 段伟韬的笔记本
* @description 针对表【blog_categories】的数据库操作Service实现
* @createDate 2025-04-15 16:31:04
*/
@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories>
    implements CategoriesService{

    @Autowired
    private ArticlesMapper articlesMapper;

    @Autowired
    private CategoriesMapper categoriesMapper;

    @Override
    public PageInfo page(int current, int size, String keyword)
    {
        LambdaQueryWrapper<Categories> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Categories::getName,"%"+keyword+"%");
        Page pages = this.page(new Page<Categories>(current,size),lambdaQueryWrapper);
        System.out.println(pages.getRecords());
        PageInfo pageInfo = new PageInfo(pages,7);
        return pageInfo;
    }

    @Transactional
    public boolean removeCategoriesByIds(int id)
    {
        //查询该分类下所有的文章
        LambdaQueryWrapper<Articles> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Articles::getCategoryId,id);
        articlesMapper.delete(lambdaQueryWrapper);
        //删除这个分类
        categoriesMapper.deleteById(id);
        return true;
    }
}




